package com.jtjrenren.android.taxi.driver.core.setting;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.jtjrenren.android.taxi.driver.bean.api.ApiDutyInfo;
import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.bean.api.ApiCommonResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiUpVersion;
import com.jtjrenren.android.taxi.driver.bean.api.CommonResult;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.driver.utils.GsonUtils;
import com.jtjrenren.android.taxi.driver.utils.IntentUtils;
import com.jtjrenren.android.taxi.driver.utils.ModuleUtils;
import com.jtjrenren.android.taxi.driver.utils.UIHelper;
import com.library.utils.AppUtils;
import com.library.utils.FileUtils;
import com.library.utils.StringUtils;
import com.library.weiget.UpAppDlg;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by Administrator on 2016/5/5.
 */
public class SettingActivity extends BaseActivity {
    @BindView(R.id.setting_lcjd_switch)
    ToggleButton tg_lcjd;
    @BindView(R.id.setting_fc_switch)
    ToggleButton tg_fc;
    @BindView(R.id.setting_hb_switch)
    ToggleButton tg_hb;
    @BindView(R.id.setting_upapk_version)
    TextView tv_version;

    private ApiDutyInfo lcjd, fc, hb;
    private UpAppDlg upVersionDlg;
    private DriverInfo driver;
    private String apkPath;
    private int processn = 0;

    @Override
    public int getLayoutID() {
        return R.layout.aty_setting;
    }

    @Override
    public void init() {
        driver = AppManger.getInstance().driver;
        ((TextView) findViewById(R.id.title_title)).setText("设置");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        initHandler();

        String versionName = AppUtils.getVersionName(this);
        tv_version.setText("V" + versionName);
    }

    @Override
    protected void onResume() {
        super.onResume();

        getDutyDetails();
    }

    @OnClick(R.id.setting_upapk)
    public void clickUpApp() {
        checkVersion();
    }

    @OnClick(R.id.setting_about)
    public void clickAbout() {
        Intent intent = new Intent(this, AppAboutActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.setting_advice)
    public void clickAdvice() {
        IntentUtils.goHtmlActivity(this, "建议", getString(R.string.url_advice));
    }

    @OnClick(R.id.setting_help)
    public void clickHelp() {
        IntentUtils.goHtmlActivity(this, "帮助", getString(R.string.url_guide));
    }

    @OnClick(R.id.setting_hjkf)
    public void clickHjkf() {
        UIHelper.showCallPhoneDialog(this, getString(R.string.kefudianhuaa));
    }

    @OnClick(R.id.setting_lcjd)
    public void clickLcjd() {
        IntentUtils.goLcjdPage(this, lcjd);
    }

    @OnClick(R.id.setting_fc)
    public void clickFc() {
        IntentUtils.goFcPage(this, fc);
    }

    @OnClick(R.id.setting_hb)
    public void clickHb() {
        IntentUtils.goHbPage(this, hb);
    }

    @OnClick(R.id.setting_exit)
    public void clickExit() {
        AppManger.getInstance().driverExit();

        IntentUtils.goLoginPage(this);
        finish();
    }

    @OnClick(R.id.setting_lcjd_switch)
    public void clickLcjdS() {
        if (lcjd != null)
            setDutySwitch("0", tg_lcjd.isChecked() ? "1" : "0");
        else
            IntentUtils.goLcjdPage(this, lcjd);
    }

    @OnClick(R.id.setting_fc_switch)
    public void clickFcS() {
        if (fc != null)
            setDutySwitch("1", tg_fc.isChecked() ? "1" : "0");
        else
            IntentUtils.goFcPage(this, fc);
    }

    @OnClick(R.id.setting_hb_switch)
    public void clickHbS() {
        if (hb != null)
            setDutySwitch("2", tg_hb.isChecked() ? "1" : "0");
        else
            IntentUtils.goHbPage(this, hb);
    }

    @Override
    protected void handMsg(Message msg) {
        super.handMsg(msg);
        switch (msg.what) {
            case 0:

                break;
            case 1:
                AppUtils.installApk(new File(apkPath), this);
                break;
            case 2:
                if (upVersionDlg != null)
                    upVersionDlg.refreshProcess(processn);
                break;
        }
    }

    private void showUpAppDialog(final ApiUpVersion.VersionEntity version, final boolean isForce) {
        if (version == null) {
            //检查更新完毕处理
            return;
        }
        upVersionDlg = UIHelper.getUpVersionDlg(this);
        upVersionDlg.setCommon(version.getVersionNO(), "", version.getContent(), new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upVersionDlg.showProcess();
                //更新
                downApp(version.getAndroidurl());
            }
        }, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        upVersionDlg.setCanceledOnTouchOutside(false);
        upVersionDlg.show();
    }


    private void checkVersion() {
        showProDialog(null, this.getString(R.string.qianchabanbenzhongqingshaohou));

        ApiWorks.checkAppVersion(new ApiWorks.ResponseListener<ApiUpVersion>() {
            @Override
            public void onResponse(ApiUpVersion upVersion) {
                disProDialog();
                if (upVersion != null) {
                    if (upVersion.getCode() == 1 && upVersion.getData().size() > 0) {
                        ApiUpVersion.VersionEntity versionEntity = upVersion.getData().get(0);
                        if (versionEntity.getIsUpdate() == 1)
                            showUpAppDialog(versionEntity, true);
                        else
                            showUpAppDialog(versionEntity, false);
                    }else
                        displayShort("已是最新版本！");
                }
            }
        });
    }

    private void downApp(String url) {
        String APK_PATH = FileUtils.getSDCardPath() + File.separator + "rrcx" + File.separator + "tmp";

        ModuleUtils.downFile(url, APK_PATH, new ModuleUtils.DownListenre() {
            @Override
            public void onProcess(int process) {
                processn = process;
                handler.sendEmptyMessage(2);
            }

            @Override
            public void onError(Throwable e) {
                handler.sendEmptyMessage(0);
            }

            @Override
            public void onComplted(String path) {
                handler.sendEmptyMessage(1);
                apkPath = path;
            }
        });
    }

    /**
     * 获取配置
     */
    private void getDutyDetails() {
        if (driver == null)
            return;
        showProDialog("", getString(R.string.shujujiazaizhongqingshaohou));
        ApiWorks.getDutyInfo(driver.getWorkNo(), new ApiWorks.ResponseListener<ApiCommonResult>() {
            @Override
            public void onResponse(ApiCommonResult apiCommonResult) {
                disProDialog();
                if (apiCommonResult != null) {
                    if (apiCommonResult != null && apiCommonResult.getCode() == Constants.STATUS_RESULT_SUCCESS) {
                        if (!StringUtils.isEmpty(apiCommonResult.getData())) {
                            try {
                                List<ApiDutyInfo> apiDutyInfos = GsonUtils.fromJsonArray(apiCommonResult.getData(), ApiDutyInfo.class);
                                refreshDutyInfo(apiDutyInfos);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
    }

    private void setDutySwitch(String dutyType, String ison) {
        if (driver == null)
            return;
        showProDialog("", getString(R.string.shezhizhongqingshaohou));
        ApiWorks.SetDriverDutyState(driver.getWorkNo(), dutyType, ison, new ApiWorks.ResponseListener<CommonResult>() {
            @Override
            public void onResponse(CommonResult commonResult) {
                disProDialog();
                if (commonResult != null && commonResult.getCode() == 1) {
                }
            }
        });
    }

    private void refreshDutyInfo(List<ApiDutyInfo> apiDutyInfos) {
        if (apiDutyInfos == null || apiDutyInfos.size() < 1)
            return;
        for (ApiDutyInfo info : apiDutyInfos) {
            if (info.getDutyType() == 0) {
                if (info.getIsOn() == 1)
                    tg_lcjd.setChecked(true);
                else
                    tg_lcjd.setChecked(false);
                lcjd = info;
            } else if (info.getDutyType() == 1) {
                if (info.getIsOn() == 1)
                    tg_fc.setChecked(true);
                else
                    tg_fc.setChecked(false);
                fc = info;
            } else if (info.getDutyType() == 2) {
                if (info.getIsOn() == 1)
                    tg_hb.setChecked(true);
                else
                    tg_hb.setChecked(false);
                hb = info;
            }
        }
    }

}
